package com.mmz.gulimall.product.web;

import com.mmz.gulimall.product.entity.CategoryEntity;
import com.mmz.gulimall.product.service.CategoryService;
import com.mmz.gulimall.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 首页接口
 * @author mangmangguai
 */
@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;


    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){
        //todo 1.查询出所有的一级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();
        //将一级分类数据传输给页面
        model.addAttribute("categorys",categoryEntities);

        return "index";
    }

    /**
     * 获取左侧菜单栏
     * @return
     */
    @ResponseBody
    @GetMapping("/index/json/catalog.json")
    public Map<String, List<Catelog2Vo>> getCategoryService() {
        return categoryService.getCategoryService();
    }
    
//    @ResponseBody
//    @GetMapping("/hello")
//    public String hello() {
//        RLock lock = redisson.getLock("my-lock");
//        try {
//            lock.lock();
//            System.out.println("加锁成功！："+Thread.currentThread().getName());
//            Thread.sleep(30000);
//        }catch (Exception e){
//            e.printStackTrace();
//        }finally {
//            lock.unlock();
//            System.out.println("释放成功！："+Thread.currentThread().getName());
//        }
//        return "hello";
//    }
//
//    @ResponseBody
//    @GetMapping("/park")
//    public String speak(){
//        RSemaphore park = redisson.getSemaphore("park");
//        boolean b = park.tryAcquire();
//        return b?"成功停车！":"车位已满！";
//    }
//
//    @ResponseBody
//    @GetMapping("/go")
//    public String go(){
//        RSemaphore park = redisson.getSemaphore("park");
//        park.release();
//        return "出库！";
//    }
}
